// {
//     // 递归实现1到100相加
//     function add(m) {
//         if (m == 2) {
//             return m + 1;
//         } else {
//             return m + add(m - 1);
//         }
//     }
//     console.log(add(100))
// }

// {
//     // 执行顺序
//     setTimeout(() => {
//         console.log('a');
//     }, 0)

//     let p = new Promise(resolve => {
//         console.log('b');
//         resolve();  // resolve和reject不能结束promise，会继续执行下边的同步任务
//         console.log('c');
//     })

//     p.then(() => {
//         console.log('d')
//     })

//     console.log('f')
//     // b c f d a
// }

// {
//     let p = new Promise(resolve => {
//         setTimeout(() => {
//             console.log('a');
//         }, 0)
//         resolve();
//         console.log('b');
//     })
//     p.then(() => {
//         console.log('c');
//     })
//     // b c a
// }
// {
//     let p = new Promise(resolve => {
//         setTimeout(() => {
//             console.log('a');
//             resolve('d');
//         }, 0)
//         console.log('b');
//     })
//     p.then((data) => {
//         console.log(data);
//     })
//     console.log('c')
//     // b c a d

// }

// {
//     const p = new Promise((resolve, reject) => {
//         console.log(1)

//         const q = () => new Promise((resolve, reject) => {
//             console.log(3)
//             resolve(4)
//         })

//         const r = () => new Promise((resolve, reject) => {
//             console.log(7)
//             resolve(8)
//         })

//         setTimeout(() => {
//             console.log(10)
//         }, 0)

//         resolve(2)

//         q().then(res => {
//             console.log(res)
//             setTimeout(() => {
//                 console.log(6)
//             },0)
//         })

//         r().then(res => {
//             console.log(res)
//             setTimeout(() => {
//                 console.log(9)
//             },0)
//         })
//     })

//     console.log(5)
//     p.then(res => {
//         console.log(res)
//     })

//     // 1 3 7 5 4 8 2 10 6 9
// }

{
  console.log("----ceshi----");
  var s = ["2,3", "4,5"];
  s = s.map(d => d.split(","));
  console.log(s)
}
{
  console.log("Start");

  setTimeout(() => {
    console.log("Timeout 1");
    Promise.resolve().then(() => {
      console.log("Promise inside Timeout 1");
    });
  }, 0);

  setTimeout(() => {
    console.log("Timeout 2");
  }, 0);

  Promise.resolve().then(() => {
    console.log("Promise 1");
    Promise.resolve().then(() => {
      console.log("Promise inside Promise 1");
    });
  });

  Promise.resolve().then(() => {
    console.log("Promise 2");
  });

  console.log("End");
  // Start
  // End
  // Promise 1
  // Promise 2
  // Promise inside Promise 1
  // Timeout 1
  // Promise inside Timeout 1
  // Timeout 2
}

// {
//     let a = {};
//     let b = function(){};
//     a.__proto__ === Object.prototype;
//     b.__proto__ === Function.prototype;

//     Function.__proto__ === Function.prototype;
//     Function.prototype.__proto__ == Object.prototype;
// }

{
  // list = [{data:['a','b','c']},1, [2, [3, 4, 5]], 6]   结果：['a', 'b', 'c', 1, 2, 3, 4, 5, 6]
  let list = [{ data: ["a", "b", "c"] }, 1, [2, [3, 4, 5]], 6];
  let newArr = [];
  function getArr(list) {
    list.forEach((arr) => {
      if (Object.prototype.toString.call(arr) === "[object Object]") {
        Object.keys(arr).forEach((d) => {
          getArr(arr[d]);
        });
      } else if (Array.isArray(arr)) {
        getArr(arr);
      } else {
        newArr.push(arr);
      }
    });
  }
  getArr(list);
  console.log(newArr);
}

// {
//     // 实现Abc123Def转换为abc123-def的函数
//     let str = 'Abc123Def';
//     let n = str.toLowerCase().replace(/(\d)([a-z])/g, '$1-$2')
//     console.log(n)
// }

{
  // 实现对象的深拷贝
  // JSON.stringify()  JSON.parse() 也可以实现深拷贝
  // 丢失数据类型 Date类型会变成字符串，正则对象会变成空对象  function Map Set等也是
  // undefined属性会被忽略去掉
  // 对象内部属性见的相互引用会报错
  // structuredClone(obj)  js新的深拷贝方法 全局
  // 不支持的有Function Symbol Dom节点
  let jack = {
    name: "jack.ma",
    age: 40,
    like: {
      dog: {
        color: "black",
        age: 3,
      },
      cat: {
        color: "white",
        age: 2,
      },
    },
    list: [
      {
        name: "Json",
        type: 1,
      },
      {
        name: "Python",
        type: 2,
      },
    ],
    n: null,
    school: {
      class: "two",
      score: 230,
      arr: [1, 2, 3, 4],
    },
  };
  function copy(dest) {
    if (typeof dest === "object") {
      if (!dest) { // null  typeof null 也是 object
        return dest;
      }
      let obj = dest.constructor(); // Object/Array
      // var obj = dest instanceof Array ? [] : {};  跟上一行代码作用相同
      for (let key in dest) {
        obj[key] = copy(dest[key]);
      }
      return obj;
    } else {
      return dest;
    }
  }
  let jack2 = copy(jack);

  // 测试
  //比如修改jack2中的内容，不会影响到jack中的值
  jack2.like.dog.color = "green";
  jack2.list[0].name = "haha";
  console.log(jack.like.dog.color);
  console.log(jack.list[0].name);

  let testArr = [
    1,
    2,
    3,
    {
      name: "Json",
      age: 18,
      family: { father: "Suene", mather: "Juyrh", money: [100, 200] },
    },
    {
      school: "jier",
      class: "343",
    },
  ];

  let copyTest = copy(testArr);
  copyTest[3].name = "UIJEnd";
  console.log("------------------testArr");
  console.log(testArr);
  console.log(copyTest);
}

{
  // 判断数据类型
  var d = undefined;
  var n = null;
  var bool = true;
  var num = 10;
  var str = "haha";
  var arr = [1, 2, 3, 4];
  var obj = { name: "sss", age: 18 };
  var func = () => {
    return null;
  };
  function School() {
    this.name = "济南大学";
    this.position = "济南";
  }
  var gread = new School();

  console.log("-----------------判断数据类型");
  console.log("----typeof");
  console.log("typeof undefined  ", typeof d);
  console.log("typeof null  ", typeof n);
  console.log("typeof bool  ", typeof bool);
  console.log("typeof num  ", typeof num);
  console.log("typeof str  ", typeof str);
  console.log("typeof arr  ", typeof arr);
  console.log("typeof obj  ", typeof obj);
  console.log("typeof func  ", typeof func); // function
  console.log("typeof School  ", typeof School); // function
  console.log("typeof gread  ", typeof gread); // object
  // typeof   只能判断基础类型   num string boolean undefined
  // 对于引用类型，除了能识别函数， 判断null,数组，对象，函数的实例（new+函数  gread）都返回object

  console.log("----instanceof");
  // console.log('d instanceof undefined  ', d instanceof undefined)   // 会报错  instanceof右侧应该是一个对象
  // console.log('n instanceof null  ', n instanceof null)

  // console.log('bool instanceof Boolean ', bool instanceof Boolean)  // false  判断错误
  // console.log('num instanceof Number  ', num instanceof Number)  // false  判断错误
  // console.log('str instanceof String ', str instanceof String)  // false  判断错误

  console.log("arr instanceof Array ", arr instanceof Array);
  console.log("obj instanceof Object ", obj instanceof Object);
  console.log("func instanceof Function ", func instanceof Function);

  //
  console.log("----通用---Object.prototype.toString.call");
  console.log("undefined", Object.prototype.toString.call(d)); // [object Undefined]
  console.log("null", Object.prototype.toString.call(n)); // [object Null]
  console.log("bool", Object.prototype.toString.call(bool)); // [object Boolean]
  console.log("arr", Object.prototype.toString.call(arr)); // [object Array]
  console.log("obj", Object.prototype.toString.call(obj)); // [object Object]
  console.log("func", Object.prototype.toString.call(func)); // [object Function]
  console.log("school", Object.prototype.toString.call(School)); // [object Function]
  console.log("gread", Object.prototype.toString.call(gread)); // [object Object]

  // 判断数组
  console.log("-----判断数组");
  var arr2 = [
    { name: "sss", age: 12 },
    { name: "eee", age: 3 },
  ];
  console.log(Array.isArray(arr2));
  console.log(arr2 instanceof Array);
  console.log(Object.prototype.toString.call(arr2) == "[object Array]");
  console.log(arr.constructor === Array);
  console.log(arr.__proto__.constructor === Array);
}
